local Msg = require("core.Msg")
local HorseConfig = require("excel.Horse")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local EquipExcel = require("excel.Equip")
local EquipScore = EquipExcel.equipScore
local needConfig = HorseConfig.other
local StageConfig = HorseConfig.stage
local HuanhuaConfig = HorseConfig.huanhua
local LvConfig = HorseConfig.lv
local Grid = require("bag.Grid")
local SkillNotify  = require("skill.Notify")
local Status = require("scene.Status")
local Skill = require("skill.Skill")
local SkillDefine = require("skill.Define")
local ObjHuman = require("core.ObjHuman")
local BufferCmds = require("buffer.Cmds")
local ShopLogic = require("shop.ShopLogic")
local HtmlUtil = require("common.HtmlUtil")
local AcvityDefine = require("activity.Define")
local RankDefine = require("rank.RankDefine")
local Log = require("common.Log")
local CoolDown = require("skill.CoolDown")
local YYDaBiao = require("activity.operate.DaBiao")
local HorseEquipQua = HorseConfig.equipQua
local HorseEquipHecheng = HorseConfig.equipHecheng
local RoleAttr = require("role.RoleAttr")
local EquipConfig = require("excel.Equip").equip
local ItemDefine = require("bag.ItemDefine")
local feedConfig = HorseConfig.feed
local HorseDefine = require("horse.HorseDefine")
local OfficialLogic = require("official.OfficialLogic")
--得到坐骑幻化信息
function getHorseSource(human)
  if human.db.isMount ~= 0 then
    for i=1, #StageConfig do
      if StageConfig[i].horseId == human.db.chooseHorseId then
        return StageConfig[i].source
      end
    end
    for k,v in pairs(HuanhuaConfig) do
      if k == human.db.chooseHorseId then
        return v.source
      end
    end
  end
  return 0 --1004
end
--得到坐骑配置公共信息
function getCommon(human, id)
  for i=1, #StageConfig do
    if StageConfig[i].horseId == id then
      return StageConfig[i]
    end
  end
  for k,v in pairs(HuanhuaConfig) do
    if k == id then
      return v
    end
  end
  return nil  
end
--得到坐骑技能
function getHorseSkill(human)
  if human.db.chooseHorseId == 0 then
      return
  end
  for i=1, #StageConfig do
    if StageConfig[i].horseId == human.db.chooseHorseId then      
       return StageConfig[i].skillId, human.db.horseSkillLv
    end
  end
  for k,v in pairs(HuanhuaConfig) do
    if k == human.db.chooseHorseId then
      local data = getHuanhuaHorseInfo(human, k)
      return v.skillId,(data and data.skillLv or 1)
    end
  end 
end

--设置坐骑信息
function setHorseInfo(human, id, info, isOther)
  if id < 20 then
    local equipData = getHorseData(human, id)
    info.horseEquip[0] = 4
    for i=1, 4 do
      setHorseEquip(human, info.horseEquip[i], id, i)
    end

    if HorseEquipQua[equipData and (equipData.equipQua + 1) or 1] then  
      info.horseEquipNeedYL = HorseEquipQua[equipData and (equipData.equipQua + 1) or 1].needYinliang
    else
      info.horseEquipNeedYL = 0
    end
    info.id = StageConfig[id].horseId
    info.name = StageConfig[id].name
    info.image = StageConfig[id].image
    info.source = StageConfig[id].source
    info.needLv = StageConfig[id].needLv
    info.xxCnt = human.db.horseXX[id-1]
    SkillNotify.setSkillData(human, StageConfig[id].skillId, info.skill, human.db.horseSkillLv, nil, isOther)
    info.curAttr[0] = #StageConfig[id].addAttr
    info.feed = isFeedFull(human, id, StageConfig[id], equipData) and 1 or 0
    for j=1, #StageConfig[id].addAttr do
      info.curAttr[j].key = StageConfig[id].addAttr[j][1]
      info.curAttr[j].value = StageConfig[id].addAttr[j][2]
    end
--    if human.db.horseXX[id - 1] ~= 0 then
--      addAttr(info.curAttr, HorseConfig.stage[id].xxAttr[human.db.horseXX[id - 1] ])
--    end
    info.power = getZhandouli(nil, info.curAttr, StageConfig[id].horseId)
    info.effect = StageConfig[id].effect
    info.isGet = human.db.horseStageLv >= id and 1 or 0
  else
    for k,v in pairs(HuanhuaConfig) do
      if k == id then
        local equipData = getHorseData(human, id)
        info.horseEquip[0] = 4
        for i=1, 4 do
          setHorseEquip(human, info.horseEquip[i], k, i)
        end
        if HorseEquipQua[equipData and (equipData.equipQua + 1) or 1] then  
          info.horseEquipNeedYL = HorseEquipQua[equipData and (equipData.equipQua + 1) or 1].needYinliang
        else
          info.horseEquipNeedYL = 0
        end
        local data = getHuanhuaHorseInfo(human, k)
        info.id = id
        info.name = v.name    
        info.isGet = isGetHuanhuaHorse(human, k)
        info.image = v.image
        info.source = v.source
        info.curAttr[0] = #v.addAttr
        info.effect = v.effect
        info.needLv = 0
        for i=1, #v.addAttr do
          info.curAttr[i].key = v.addAttr[i][1]
          info.curAttr[i].value = v.addAttr[i][2]
        end
--        if data and data.horseXX > 0 then
--          addAttr(info.curAttr, HuanhuaConfig[k].xxAttr[data.horseXX])
--        end
        info.feed = isFeedFull(human, id, HuanhuaConfig[k], equipData) and 1 or 0
        info.power = getZhandouli(nil, info.curAttr, id)
        if info.isGet ~= 0 then
          SkillNotify.setSkillData(human, v.skillId, info.skill, data.skillLv, nil, isOther)
        else
          SkillNotify.setSkillData(human, v.skillId, info.skill, 0, nil, isOther)
        end
        info.xxCnt = data and data.horseXX or 0
      end
    end
  end
end

--进阶坐骑所有信息
function sendHorseList(human)
  local sendMsg = Msg.gc.GC_HORSE_STAGE_LIST
  for i=1, #StageConfig do
    setHorseInfo(human, i, sendMsg.list[i])
  end
  sendMsg.feedYinliang = HorseDefine.FEED_NEED_YINLIANG
  sendMsg.list[0] = #HorseConfig.stage
  Msg.send(sendMsg, human.fd)
end
function getHuanhuaHorseInfo(human, id)
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == id then
      return human.db.haveHorse[i]
    end
  end
end
--判断是否获得幻化坐骑
function isGetHuanhuaHorse(human, id)
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == id then
      if human.db.haveHorse[i].startTime == 0 or (human.db.haveHorse[i].startTime ~= 0 and human.db.haveHorse[i].limitTime ~= 0 
        and (os.time() - human.db.haveHorse[i].startTime) < human.db.haveHorse[i].limitTime) then
        return 1
      end
    end
  end
  return 0
end
--限时坐骑设为未获得状态
function clearHuanhuaHorse(human, sid, isWar)
  local id = 0
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == sid then
      human.db.haveHorse[i].limitTime = 0
      id = human.db.haveHorse[i].haveHorseId
      break
    end
  end
  if id == 0 then
    return
  end
  if human.db.chooseHorseId == id then
    human.db.chooseHorseId = StageConfig[human.db.horseStageLv].horseId
    SkillNotify.GC_SKILL_LIST(human)
    SkillNotify.GC_SKILL_LIST_CHANGE(human)
    SkillNotify.GC_SKILL_MAIN_MENU(human)
  end
  ObjHuman.doCalc(human)  
  
  if isWar then 
      return
  end
  sendHorseStatus(human)
  sendHorseInfo(human)
  updateHuanhua(human, id)  
end
--限时坐骑时间到了，清空时间和id
function setHuanhuaRemainTime(human)
  local id = 0
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].startTime~=0 and human.db.haveHorse[i].limitTime ~= 0 and human.db.haveHorse[i].limitTime < (os.time()-human.db.haveHorse[i].startTime) then
      human.db.haveHorse[i].limitTime = 0
      id = human.db.haveHorse[i].haveHorseId
      break
    end
  end
  if id == 0 then
    return
  end
  
  if human.db.chooseHorseId == id then
    human.db.chooseHorseId = StageConfig[human.db.horseStageLv].horseId
    SkillNotify.GC_SKILL_LIST(human)
    SkillNotify.GC_SKILL_LIST_CHANGE(human)
    SkillNotify.GC_SKILL_MAIN_MENU(human)
  end
  ObjHuman.doCalc(human)  
  sendHorseStatus(human)
  sendHorseInfo(human)
  updateHuanhua(human, id)  
end
--得到幻化坐骑限时时间
function getHuanhuaRemainTime(human, id)
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == id and  human.db.haveHorse[i].startTime ~= 0 then
      if human.db.haveHorse[i].limitTime - (os.time() - human.db.haveHorse[i].startTime) > 0 then
        return human.db.haveHorse[i].limitTime - (os.time() - human.db.haveHorse[i].startTime)    
      end
    end
  end
  return 0
end
--赋值装备信息
function setHorseEquip(human, info, horseId, index)
  local data = getHorseData(human, horseId)
  local quality = 1
  if data then
    quality = data.equipQua + 1
  end
  local cache = {}
  
  if data and data.equipId[index - 1] ~= 0 then
    info.curEquip[0] = 1
    Grid.create(cache, data.equipId[index - 1], 1)
    Grid.makeItem(cache, info.curEquip[1], 0)
  elseif HorseEquipQua[quality - 1] then
    info.curEquip[0] = 1
    Grid.create(cache, HorseEquipQua[quality - 1].needEquip[index], 1)
    Grid.makeItem(cache, info.curEquip[1], 0)
  else
    info.curEquip[0] = 0
  end
  
  info.isGet = 0
  if data and data.equipId[index - 1] ~= 0 then
    info.isGet = 1
  end
  if HorseEquipQua[quality] then
    info.nextEquip[0] = 1
    Grid.create(cache, HorseEquipQua[quality].needEquip[index], 1)
    Grid.makeItem(cache, info.nextEquip[1], 0)
    local need = HorseEquipHecheng[HorseEquipQua[quality].needEquip[index]]
    info.needItem[0] = #need.needItem
    for i=1, info.needItem[0] do
      Grid.create(cache, need.needItem[i][1], need.needItem[i][2])
      Grid.makeItem(cache, info.needItem[i], 0)
    end
    info.needHorseLv = need.needHorseLv
    info.needYinliang = need.needYinliang
  else
    info.nextEquip[0] = 0
    info.needItem[0] = 0
    info.needHorseLv = 0
    info.needYinliang = 0
  end
end

--组织幻化信息
function getHuanhuaInfo(human, k, v, info)
    local cache = {}
    local data = getHuanhuaHorseInfo(human, k)
    if data and data.skillLv == 0 then
      data.skillLv = 1
    end
    local equipData = getHorseData(human, k)
    info.horseEquip[0] = 4
    for i=1, 4 do
      setHorseEquip(human, info.horseEquip[i], k, i)
    end
    if HorseEquipQua[equipData and (equipData.equipQua + 1) or 1] then  
      info.horseEquipNeedYL = HorseEquipQua[equipData and (equipData.equipQua + 1) or 1].needYinliang
    else
      info.horseEquipNeedYL = 0
    end
    info.id = k
    info.name = v.name
    info.isGet = isGetHuanhuaHorse(human, k)
    info.image = v.image
    info.source = v.source    
    Grid.create(cache, v.needItem[1], v.needItem[2])
    Grid.makeItem(cache, info.needItem, 0, human)
    info.needHorseLv = v.needLv
    info.curAttr[0] = #v.addAttr
    info.effect = v.effect
    for i=1, #v.addAttr do
      info.curAttr[i].key = v.addAttr[i][1]
      info.curAttr[i].value = v.addAttr[i][2]
    end
    info.feed = isFeedFull(human, k, HuanhuaConfig[k], equipData) and 1 or 0
--    if data and data.horseXX > 0 then
--      addAttr(info.curAttr, HuanhuaConfig[k].xxAttr[data.horseXX])
--    end
    info.power = getZhandouli(nil, info.curAttr, k)
    if info.isGet ~= 0 then
      SkillNotify.setSkillData(human, v.skillId, info.skill, data and data.skillLv or 1)
    else
      SkillNotify.setSkillData(human, v.skillId, info.skill, 0)
    end
    info.remainTime = getHuanhuaRemainTime(human, k)
    info.getWay = v.getWay
    local tmpConfig = HuanhuaConfig[k]
    local xxCnt = data and data.horseXX or 0
    info.xxCnt = xxCnt
    if xxCnt < 5 and tmpConfig then    
      calCurAddAttr(human, info.nextAttr)
      info.curTotalPower = getZhandouli(human, info.nextAttr, k)
      if xxCnt == 0 then
        addAttr(info.nextAttr, tmpConfig.xxAttr[xxCnt+1])
      else
        for i=1,#tmpConfig.xxAttr[xxCnt+1] do
          local realAddAttr = {}
          realAddAttr[1] = tmpConfig.xxAttr[xxCnt+1][i][1]
          realAddAttr[2] = tmpConfig.xxAttr[xxCnt+1][i][2] - tmpConfig.xxAttr[xxCnt][i][2]
          RoleAttr.addOneAttr(info.nextAttr, realAddAttr)
        end
      end 
      info.nextPower = getZhandouli(human, info.nextAttr, k)
      info.needXXYinliang = tmpConfig.xxNeedYinliang[xxCnt + 1]
      info.needXXItem[0] = 1
      Grid.create(cache, tmpConfig.xxNeedItem[xxCnt + 1][1], tmpConfig.xxNeedItem[xxCnt + 1][2])
      Grid.makeItem(cache, info.needXXItem[1], 0, human)
    else
      info.curTotalPower = 0
      info.nextAttr[0] = 0
      info.nextPower = 0
      info.needXXYinliang = 0
      info.needXXItem[0] = 0
    end
    if xxCnt == 5 then
      info.needXXItem[0] = 1
      Grid.create(cache, tmpConfig.xxNeedItem[xxCnt][1], tmpConfig.xxNeedItem[xxCnt][2])
      Grid.makeItem(cache, info.needXXItem[1], 0, human)
    end
end
--发送幻化坐骑信息
function sendHuanhuaList(human)
  local sendMsg = Msg.gc.GC_HORSE_HUANHUA_INFO
  local index = 0
  for k,v in pairs(HuanhuaConfig) do
    index = index + 1
    getHuanhuaInfo(human, k, v, sendMsg.list[index])    
  end
  sendMsg.list[0] = index
  Msg.send(sendMsg, human.fd)
end
--更新单个幻化坐骑信息
function updateHuanhua(human, id)
  local sendMsg = Msg.gc.GC_HORSE_UPDATE_HUANHUA
  for k,v in pairs(HuanhuaConfig) do
    if k == id then
      getHuanhuaInfo(human, k, v, sendMsg.info)
    end
  end
  Msg.send(sendMsg, human.fd)
end
--发送坐骑状态
function sendHorseStatus(human)
  local sendMsg = Msg.gc.GC_HORSE_STATUS
  sendMsg.status = human.db.isMount
  sendMsg.id = human.db.chooseHorseId
  if HuanhuaConfig[human.db.chooseHorseId] then
    sendMsg.source = HuanhuaConfig[human.db.chooseHorseId].source
  else
    for i=1, #StageConfig do
      if StageConfig[i].horseId == human.db.chooseHorseId then
        sendMsg.source = StageConfig[i].source
      end  
    end
    sendMsg.source = sendMsg.source or 0
  end
  Msg.send(sendMsg, human.fd)
  Status.sendViewStatus(human, true)
end
--上下坐骑
function mountHorse(human)
  local mount = (human.db.isMount + 1) % 2
  if mount == 1 then
    if BufferCmds.passive.luoma(human) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_NOT_MOUNT_LUOMA)
        return
    end
    Skill.use(human, SkillDefine.SKILL_ON_HORSE_ID, human.x, human.y, human.x, human.y, human)
  else
    human.db.isMount = mount 
    sendHorseStatus(human)
    ObjHuman.doCalc(human)
    SkillNotify.GC_SKILL_LIST(human)
    SkillNotify.GC_SKILL_LIST_CHANGE(human)
    SkillNotify.GC_SKILL_MAIN_MENU(human)
  end 
end
--将坐骑属相加到人物身上
function doCalc(human, send)
  if BufferCmds.passive.luoma(human) then
      return
  end
  local attrs = {}
  calCurAddAttr(human, attrs)
  
  for i = 1, attrs[0] do
    if not send then
      human.attr[attrs[i].key] = human.attr[attrs[i].key] + attrs[i].value
    end
  end
  if send then
    return attrs
  end
end
--计算战斗力
function getZhandouli(human, attrs, id)
  local zhandouli = 0
  for i = 1, attrs[0] do
    zhandouli = zhandouli + attrs[i].value * EquipScore[attrs[i].key].score
  end
  if human then
    return zhandouli + getSkillZhandouli(human, id)
  else
    return zhandouli
  end
end
--
function addOneAttrByKey(attrs, attrOne)
  for i=1, attrs[0] do
    if attrOne.key == attrs[i].key then
      attrs[i].value = attrs[i].value + attrOne.value
      return
    end
  end
  attrs[0] = attrs[0] + 1
  if not attrs[attrs[0] ] then
    attrs[attrs[0] ] = {}
  end
  attrs[attrs[0] ].key = attrOne.key
  attrs[attrs[0] ].value = attrOne.value
end
--累加属性
function addAttr(attrs, addAttr)
  for i=1, #addAttr do
    RoleAttr.addOneAttr(attrs, addAttr[i])
  end
end
--属性值累加
function calCurAddAttr(human, attrs)
  --赋初始值
  attrs[0] = 0  
  local starLv = human.db.horseStarLv
  for i=1, math.floor(starLv/10) + 1 do
    --星星属性
    for j=1, 10 do
      if (i - 1) * 10 + j <= starLv then
        addAttr(attrs, HorseConfig.lv[i].addStarAttr)
      end
    end    
  end
  --等级属性
  local lv = math.floor(starLv / 10) + 1
  if math.floor(starLv/100) == human.db.horseStageLv then
    lv = lv - 1
  end
  addAttr(attrs, HorseConfig.lv[lv].addAttr)
  --阶段属性
  addAttr(attrs, HorseConfig.stage[human.db.horseStageLv].addAttr)
  --幻化坐骑属性
  for i=0, human.db.haveHorseLen - 1 do
    local data = human.db.haveHorse[i]
    if isGetHuanhuaHorse(human, human.db.haveHorse[i].haveHorseId) == 1 then
      addAttr(attrs, HuanhuaConfig[data.haveHorseId].addAttr)
      if data.horseXX > 0 then
        addAttr(attrs, HuanhuaConfig[data.haveHorseId].xxAttr[data.horseXX])
      end
      for j=0, 3 do
        if data.equipId[j] ~= 0 then
          RoleAttr.addAttrs(attrs, EquipConfig[data.equipId[j]].baseAttr)
        elseif HorseEquipQua[data.equipQua] then
          RoleAttr.addAttrs(attrs, EquipConfig[HorseEquipQua[data.equipQua].needEquip[j+1]].baseAttr)
        end
      end
      if HorseEquipQua[data.equipQua] then
        RoleAttr.addAttrs(attrs, HorseEquipQua[data.equipQua].addAttr)
      end
      for j = 1, #HuanhuaConfig[data.haveHorseId].feedAttr do
        local feedAttrTemp = {}
        feedAttrTemp[1] = HuanhuaConfig[data.haveHorseId].feedAttr[j][1]
        feedAttrTemp[2] = data.feedAttr[j-1]
        RoleAttr.addOneAttr(attrs, feedAttrTemp)
      end
    end
  end
  --星星加成
  for i=1, human.db.horseStageLv do
    if human.db.horseXX[i - 1] > 0 then
      addAttr(attrs, HorseConfig.stage[i].xxAttr[human.db.horseXX[i - 1] ])
    end
    local data = getHorseData(human, i)
    if data then
      for j=0, 3 do
        if data.equipId[j] ~= 0 then
          RoleAttr.addAttrs(attrs, EquipConfig[data.equipId[j]].baseAttr)
        elseif HorseEquipQua[data.equipQua] then
          RoleAttr.addAttrs(attrs, EquipConfig[HorseEquipQua[data.equipQua].needEquip[j + 1]].baseAttr)
        end
      end
      if HorseEquipQua[data.equipQua] then
        RoleAttr.addAttrs(attrs, HorseEquipQua[data.equipQua].addAttr)
      end
      for j=1, #StageConfig[i].feedAttr do
        if data.feedAttr[j-1] > 0 then
          local feedAttrTemp = {}
          feedAttrTemp[1] = StageConfig[j].feedAttr[j][1]
          feedAttrTemp[2] = data.feedAttr[j-1]
          RoleAttr.addOneAttr(attrs, feedAttrTemp)
        end
      end
    end    
  end  
end

function getHorseLv(human)
  local starLv = human.db.horseStarLv
  local lv = math.floor(starLv / 10) + 1
  local stage = human.db.horseStageLv
  if math.floor(starLv/100) == stage then
    lv = lv - 1
  end
  return lv
end
--额外技能等级战斗力
function getSkillZhandouli(human, id)
  local value = 0
  for i=1, #StageConfig do
    if StageConfig[i].horseId == human.db.chooseHorseId then
       value = value + human.db.horseSkillLv*30
       break
    end
  end
  for k in pairs(HuanhuaConfig) do
    local info = getHuanhuaHorseInfo(human, k)
    if isGetHuanhuaHorse(human, k) == 1 and k == id then
      value = value + info.skillLv*30
      break
    end
  end 
  return (value == 0) and 30 or value
end
--发送当前坐骑页面信息
function sendHorseInfo(human, multi)
  local sendMsg = Msg.gc.GC_HORSE_INFO
  local starLv = human.db.horseStarLv
  local lv = getHorseLv(human)  
  local stage = human.db.horseStageLv
  
  sendMsg.curUpVal = human.db.horseUpVal
  sendMsg.needUpVal = LvConfig[lv].needUpVal
  sendMsg.starLv = starLv
  sendMsg.stageLv = stage
  sendMsg.multi = multi or 0
  local cache = {}
  if math.floor(starLv / 100) == stage and stage < #StageConfig then
    Grid.create(cache, StageConfig[stage + 1].needItem[1], StageConfig[stage + 1].needItem[2])
    Grid.makeItem(cache, sendMsg.StageUpItem, 0, human)   
  else
    Grid.create(cache, needConfig[1].itemId, 1)
    Grid.makeItem(cache, sendMsg.StageUpItem, 0, human)   
  end
  
  --当前属性
  calCurAddAttr(human, sendMsg.curAttr)  
  sendMsg.power = getZhandouli(human, sendMsg.curAttr, human.db.chooseHorseId)
  sendMsg.curWishVal = human.db.horseWishVal
  sendMsg.needWishVal = StageConfig[stage].needWish
  --下一阶段属性
  if starLv <= 1000 then
    sendMsg.nextStarAttr[0] = 0
    for i=1, #LvConfig[lv].addStarAttr do
      RoleAttr.addOneAttr(sendMsg.nextStarAttr, LvConfig[lv].addStarAttr[i])
    end
    if starLv % 10 == 9 and math.floor(starLv / 100) ~= stage then
      for i=1, #LvConfig[lv].addAttr do      
        sendMsg.nextStarAttr[i].value = sendMsg.nextStarAttr[i].value + LvConfig[lv + 1].addAttr[i][2] - LvConfig[lv].addAttr[i][2]  
      end    
    end
    for i=1,sendMsg.curAttr[0] do
      addOneAttrByKey(sendMsg.nextStarAttr, sendMsg.curAttr[i])
    end
    sendMsg.nextStarPower = getZhandouli(human, sendMsg.nextStarAttr, human.db.chooseHorseId)
        
    if StageConfig[stage + 1] then --第十阶不发送
      sendMsg.nextStageAttr[0] = #StageConfig[stage + 1].addAttr
      for i=1, #StageConfig[stage + 1].addAttr do
        sendMsg.nextStageAttr[i].key = StageConfig[stage + 1].addAttr[i][1]
        sendMsg.nextStageAttr[i].value = StageConfig[stage + 1].addAttr[i][2] - StageConfig[stage].addAttr[i][2]
      end
      for i=1,sendMsg.curAttr[0] do
        addOneAttrByKey(sendMsg.nextStageAttr, sendMsg.curAttr[i])
      end
      for i=1, #LvConfig[lv].addAttr do
        local temp ={}
        temp[1] = LvConfig[lv + 1].addAttr[i][1]
        temp[2] = LvConfig[lv + 1].addAttr[i][2] - LvConfig[lv].addAttr[i][2]
        RoleAttr.addOneAttr(sendMsg.nextStageAttr, temp)   
--        sendMsg.nextStageAttr[i].value = sendMsg.nextStageAttr[i].value +           
      end
      sendMsg.nextStagePower = getZhandouli(human, sendMsg.nextStageAttr, human.db.chooseHorseId)
      sendMsg.showRand = StageConfig[stage + 1].showRand
    else
      sendMsg.nextStageAttr[0] = 0
      sendMsg.nextStagePower = 0
      sendMsg.showRand = 0
    end
  else
    sendMsg.nextStarAttr[0] = 0
    sendMsg.nextStageAttr[0] = 0
    sendMsg.nextStarPower = 0
    sendMsg.nextStagePower = 0
  end
  
  local powerMsg = Msg.lw.LW_CHECK_RANK_CHANGE
  powerMsg.type = RankDefine.HORSE_RANK_TYPE
  powerMsg.otherInfo = sendMsg.power
  Msg.send2world(powerMsg, human._id)
  Msg.send(sendMsg, human.fd)
end
--增加升级值
function addUpVal(human, itemIndex)
  local multiAdd = math.random(1, 10000)
  local multi = 0 --随机到几倍暴击
  for i=1, #needConfig[itemIndex].critWeight do
    multi = multi + needConfig[itemIndex].critWeight[i][2]
    if multiAdd <= multi then
      multi =needConfig[itemIndex].critWeight[i][1]
      break
    end
  end 
  human.db.horseUpVal = human.db.horseUpVal + multi * needConfig[itemIndex].addUpVal
  for i=1, 100 do  --不用while防止死循环
    if human.db.horseUpVal >= HorseConfig.lv[math.floor((human.db.horseStarLv)/10) + 1].needUpVal then    
      human.db.horseUpVal = human.db.horseUpVal - HorseConfig.lv[math.floor(human.db.horseStarLv/10) + 1].needUpVal
      human.db.horseStarLv = human.db.horseStarLv + 1
      if math.floor(human.db.horseStarLv / 100) == human.db.horseStageLv then
        human.db.horseUpVal = HorseConfig.lv[math.floor(human.db.horseStarLv/10)].needUpVal
        break
      end
      if human.db.horseStarLv == 14 then
        human.db.horseSkillLv = 1
        SkillNotify.GC_SKILL_LIST(human)
        SkillNotify.GC_SKILL_LIST_CHANGE(human)
        SkillNotify.GC_SKILL_MAIN_MENU(human)
        SkillNotify.GC_SKILL_NEW(human, 6,StageConfig[1].skillId)
        sendHorseList(human) 
      end
      ObjHuman.doCalc(human)
    else
      break
    end
  end
  OfficialLogic.checkOneCondition(human, "horseLv")
  return multi
end
--检查是否可使用当前升星道具
function checkStarUpItem(human, itemIndex)
  if human.db.horseStageLv >= 8 and itemIndex == 3 then
    return true
  elseif human.db.horseStageLv >=4 and itemIndex >= 2 then
    return true
  elseif human.db.horseStageLv < 4 then
    return true
  end
  return false
end
--升星
function starUp(human, autoBuy, itemIndex)  
  local starLv = human.db.horseStarLv
  if starLv >= 1000 then
    return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_MAXLV)
  end
  if math.floor(human.db.horseStarLv / 100) == human.db.horseStageLv then
    return --坐骑可以进阶了，不能升级了
  end
  local beforeLv = getHorseLv(human)
  if not checkStarUpItem(human, itemIndex) then
    return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_ITEM_NOT_CAN_USE)
  end
  if BagLogic.getItemCount(human, needConfig[itemIndex].itemId) < 1 then
    if autoBuy ~= 1 then
      return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_SHENGJIDAN_NOT_ENOUGH)
    else
      if not ShopLogic.quickBuy(human, needConfig[itemIndex].itemId, 1) then
        return
      end
    end
  end  
  BagLogic.delItemsById(human, needConfig[itemIndex].itemId, 1, "horse")
  YYDaBiao.checkCanGet(human, AcvityDefine.OPERATEACT_TYPE_HORSE)
  local multi = addUpVal(human, itemIndex)
  sendHorseInfo(human, multi)
  Log.write(Log.LOGID_OSS_HORSE_STAR, human._id, human.account, human.name, human.db.lv,beforeLv, getHorseLv(human), human.db.horseStageLv,needConfig[itemIndex].itemId,1)
end
--加祝福值
function addWishVal(human)
  local stageLv = human.db.horseStageLv
  local stageConfig = StageConfig[stageLv + 1]   
  human.db.upStageTimes = human.db.upStageTimes + 1
  local times = human.db.upStageTimes
  local useRand = 0
  if times <= stageConfig.gdNeedTimes then
    useRand = stageConfig.gdRand
  elseif times > stageConfig.gdNeedTimes and times <= stageConfig.bdNeedTimes then
    useRand = stageConfig.realRand
  else
    human.db.horseWishVal = 0
    human.db.upStageTimes = 0
    human.db.horseStageLv = human.db.horseStageLv + 1
    return
  end
  if math.random(1, 10000) < useRand then
    human.db.horseWishVal = 0
    human.db.upStageTimes = 0
    human.db.horseStageLv = human.db.horseStageLv + 1
    return
  end
  local getWishVal = math.random(stageConfig.getWish[1], stageConfig.getWish[2])
  human.db.horseWishVal = human.db.horseWishVal + getWishVal
  if human.db.horseWishVal >= stageConfig.needWish then
    human.db.horseWishVal = 0
    human.db.upStageTimes = 0
    human.db.horseStageLv = human.db.horseStageLv + 1
  end
end
--升阶坐骑
function stageUp(human, autoBuy)
  local stageLv = human.db.horseStageLv
  if math.floor(human.db.horseStarLv / 100) ~= human.db.horseStageLv and human.db.horseStageLv < #StageConfig then
    return --坐骑不可以进阶
  end
  if human.db.lv < StageConfig[stageLv + 1].needLv then
    return --坐骑不可以进阶
  end
  local beforeLv = getHorseLv(human)
  local NextStageConfig = HorseConfig.stage[stageLv + 1]  
  if BagLogic.getItemCount(human, NextStageConfig.needItem[1]) < NextStageConfig.needItem[2] then
    if autoBuy ~= 1 then
      return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_JINJIEDAN_NOT_ENOUGH)
    else
      if not ShopLogic.quickBuy(human, NextStageConfig.needItem[1], NextStageConfig.needItem[2] - BagLogic.getItemCount(human, NextStageConfig.needItem[1])) then
        return
      end
    end
  end
  BagLogic.delItemsById(human, NextStageConfig.needItem[1], NextStageConfig.needItem[2], "horse") 
  addWishVal(human)
  if stageLv ~= human.db.horseStageLv then    
    human.db.horseUpVal = 0
    human.db.chooseHorseId = StageConfig[human.db.horseStageLv].horseId
    SkillNotify.GC_SKILL_LIST(human)
    SkillNotify.GC_SKILL_LIST_CHANGE(human)
    SkillNotify.GC_SKILL_MAIN_MENU(human)
    ObjHuman.doCalc(human)  
    sendHorseList(human)
    sendHorseStatus(human)
    local bro = string.format(Lang.HORSE_SHENGGJIE_BROCAST, HtmlUtil.fontName(human.name, human._id), HorseConfig.stage[stageLv].name, HorseConfig.stage[stageLv + 1].name)
    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, bro)
  end
  sendHorseInfo(human)
  Log.write(Log.LOGID_OSS_HORSE_STAGE, human._id, human.account, human.name,human.db.lv, beforeLv, stageLv, getHorseLv(human), human.db.horseStageLv, NextStageConfig.needItem[1], NextStageConfig.needItem[2]) 
end
--激活幻化坐骑
function jihuo(human, id, notDelItem)
  local huanhuaHorse = HorseConfig.huanhua[id]
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == id then
      if human.db.haveHorse[i].startTime == 0 then
        return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_HAVE_GET_THIS_HUANHUA)
      end
    end
  end
  if getHorseLv(human) < huanhuaHorse.needLv then
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_LV_NOT_ENOUGH)
    return false
  end
  if not notDelItem then
      if BagLogic.getItemCount(human, huanhuaHorse.needItem[1]) < huanhuaHorse.needItem[2] then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_HUANHUA_ITEM_NOT_ENOUGH)
        return false
      end
      BagLogic.delItemsById(human, huanhuaHorse.needItem[1], huanhuaHorse.needItem[2], "horse")
  end
  local isHave = false
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == id then
      human.db.haveHorse[i].startTime = 0
      human.db.haveHorse[i].limitTime = 0
      isHave = true
      break
    end
  end
  if not isHave then
    human.db.haveHorse[human.db.haveHorseLen].startTime = 0
    human.db.haveHorse[human.db.haveHorseLen].limitTime = 0
    human.db.haveHorse[human.db.haveHorseLen].haveHorseId = id
    human.db.haveHorse[human.db.haveHorseLen].skillLv = 1
    human.db.haveHorseLen = human.db.haveHorseLen + 1   
  end

  sendHorseInfo(human)   
  updateHuanhua(human, id)  
  ObjHuman.doCalc(human)
  Log.write(Log.LOGID_OSS_HORSE_JIHUO, human._id, human.account, human.name,human.db.lv, getHorseLv(human), human.db.horseStageLv, id)
  return true
end

--激活限时幻化坐骑
function jihuoLimit(human, id, limitTime, itemId, itemCnt, isWar, noSend)
  local huanhuaHorse = HorseConfig.huanhua[id]
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == id then
      if human.db.haveHorse[i].startTime == 0 then
        -- 已经有这个永久坐骑
        if isWar then
          return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_HAVE_GET_THIS_HUANHUA)
        end
        
        if not BagLogic.checkCanAddItem(human, itemId, true, itemCnt) then
          return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_BAG_NOT_EUOUGH_NOT_CAN_USE)
        else
          BagLogic.addItem(human, itemId, true, itemCnt, "horse")
          return true
        end
      elseif human.db.haveHorse[i].limitTime == 0 then
        -- 曾经激活过 但是现在已经过期
        
      elseif human.db.haveHorse[i].limitTime > (os.time()-human.db.haveHorse[i].startTime) then
        -- 曾经激活过 但是现在已经过期
        
      else
        -- 当前限时激活中
        if isWar then
          return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_HAVE_GET_THIS_HUANHUA)
        end
        
        if not BagLogic.checkCanAddItem(human, itemId, true, itemCnt) then
          return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_BAG_NOT_EUOUGH_NOT_CAN_USE)
        else
          BagLogic.addItem(human, itemId, true, itemCnt, "horse")
          return true
        end
      end
    end
  end
  if getHorseLv(human) < huanhuaHorse.needLv then
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_LV_NOT_ENOUGH)
    return false
  end
  local isHave = false
  for i=0, human.db.haveHorseLen - 1 do
    if human.db.haveHorse[i].haveHorseId == id then
      human.db.haveHorse[i].startTime = os.time()
      human.db.haveHorse[i].limitTime = limitTime
      isHave = true
      break
    end
  end
  if not isHave then
    human.db.haveHorse[human.db.haveHorseLen].haveHorseId = id
    human.db.haveHorse[human.db.haveHorseLen].startTime = os.time()
    human.db.haveHorse[human.db.haveHorseLen].limitTime = limitTime
    human.db.haveHorse[human.db.haveHorseLen].skillLv = 1
    human.db.haveHorseLen = human.db.haveHorseLen + 1
  end
  if human.db.haveHorseLen > 10 then
    human.db.haveHorseLen = 10
  end
  huanhua(human, id, noSend)  
  if noSend then
    return
  end  
  sendHorseInfo(human)
  updateHuanhua(human, id)  
  ObjHuman.doCalc(human)  
  return true
end

--判断是否得到此坐骑
function isGetStageHorse(human, id)
  for i=1, #StageConfig do
    if StageConfig[i].horseId == id and human.db.horseStageLv >= i then
      return 1
    end
  end
  return 0
end
--设置坐骑技能冷却
function setSkillColldDown(human)
  if human.db.horseSkillLv > 0 then
    for i=1, #StageConfig do
      if StageConfig[i].horseId == human.db.chooseHorseId then
        CoolDown.doCoolDown(human, StageConfig[i].skillId)
        return
      end
    end
  end
  for k,v in pairs(HuanhuaConfig) do
    if k == human.db.chooseHorseId then        
      CoolDown.doCoolDown(human, v.skillId)
      return
    end
  end
end
--幻化坐骑形象
function huanhua(human, id, noSend)
  if not (isGetHuanhuaHorse(human, id) == 1 or isGetStageHorse(human, id) == 1) then
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_NOT_GET_THIS_HUANHUA) 
  end
  if ObjHuman.checkIsInFighting(human) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_IS_COMBATING)
  end
  human.db.chooseHorseId = id
  if noSend then
    return 
  end
  setSkillColldDown(human)
  sendHorseInfo(human)
  sendHorseStatus(human)
  ObjHuman.doCalc(human)
  SkillNotify.GC_SKILL_LIST(human)
  SkillNotify.GC_SKILL_LIST_CHANGE(human)
  SkillNotify.GC_SKILL_MAIN_MENU(human)
end
--检查批量使用道具是否重复
function checkRepeat(msg, new)
  local isHave = false
  for i=1, msg[0] do
    if msg[i].mutli == new then
      msg[i].times = msg[i].times + 1
      isHave = true
    end
  end
  if not isHave then
    msg[0] = msg[0] + 1
    msg[msg[0] ].mutli = new
    msg[msg[0] ]. times = 1
  end
end
--批量使用道具
function batchUse(human, cnt, isAutoBuy, itemIndex)
  if not checkStarUpItem(human, itemIndex) then
    return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_ITEM_NOT_CAN_USE)
  end
  if BagLogic.getItemCount(human, needConfig[itemIndex].itemId) < cnt then
    if isAutoBuy ~= 1 then
      return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_SHENGJIDAN_NOT_ENOUGH)
    else
      if not ShopLogic.quickBuy(human, needConfig[itemIndex].itemId, cnt - BagLogic.getItemCount(human, needConfig[itemIndex].itemId)) then
        return
      end
    end
  end
  local sendMsg = Msg.gc.GC_HORSE_BATCH_USE_ITEM
  sendMsg.times[0] = 0
  local totalExp = 0
  local itemCnt = 0
  local beforeLv = getHorseLv(human)
  while cnt > 0 do
    local starLv = human.db.horseStarLv
    if starLv >= 1000 then
      return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_MAXLV)
    end
    if math.floor(human.db.horseStarLv / 100) == human.db.horseStageLv then
      break --坐骑可以进阶了，不能升级了
    end  
    local multi = addUpVal(human, itemIndex)
    totalExp = totalExp + needConfig[itemIndex].addUpVal * multi
    checkRepeat(sendMsg.times, multi)
    cnt = cnt - 1
    itemCnt = itemCnt + 1
  end
  BagLogic.delItemsById(human, needConfig[itemIndex].itemId, itemCnt, "horse")  
  sendMsg.itemCnt = itemCnt
  sendMsg.totalExp = totalExp
  Msg.send(sendMsg, human.fd)
  sendHorseInfo(human)
  YYDaBiao.checkCanGet(human, AcvityDefine.OPERATEACT_TYPE_HORSE)
  OfficialLogic.checkOneCondition(human, "horseLv")
  Log.write(Log.LOGID_OSS_HORSE_STAR, human._id, human.account, human.name,human.db.lv, beforeLv, getHorseLv(human), human.db.horseStageLv, needConfig[itemIndex].itemId, itemCnt)
end
--是否可进阶，是返回true，不可进阶返回false
function CanStage(human)
  if math.floor(human.db.horseStarLv / 100) == human.db.horseStageLv then
    return true
  end
  return false
end
--增加星级，cnt为增加几星
function addStarLv(human, cnt)
  human.db.horseStarLv = human.db.horseStarLv + cnt
  if human.db.horseStarLv / 100 >= human.db.horseStageLv then
    human.db.horseStarLv = human.db.horseStageLv * 100
  end  
  sendHorseInfo(human)
end
--增加等级,cnt为增加几级
function addLv(human, cnt)
  human.db.horseStarLv = human.db.horseStarLv + cnt * 10
  if human.db.horseStarLv / 100 >= human.db.horseStageLv then
    human.db.horseStarLv = human.db.horseStageLv * 100
  end
  OfficialLogic.checkOneCondition(human, "horseLv")
  sendHorseInfo(human)
end
--增加升级值，
function addExp(human, exp)
  human.db.horseUpVal = human.db.horseUpVal + exp
  for i=1, 100 do  --不用while防止死循环
    if human.db.horseUpVal >= HorseConfig.lv[math.floor((human.db.horseStarLv)/10) + 1].needUpVal then    
      human.db.horseUpVal = human.db.horseUpVal - HorseConfig.lv[math.floor(human.db.horseStarLv/10) + 1].needUpVal
      human.db.horseStarLv = human.db.horseStarLv + 1
      if math.floor(human.db.horseStarLv / 100) == human.db.horseStageLv then
        human.db.horseUpVal = HorseConfig.lv[math.floor(human.db.horseStarLv/10)].needUpVal
        break
      end
      ObjHuman.doCalc(human)
    else
      break
    end
  end
  OfficialLogic.checkOneCondition(human, "horseLv")
  sendHorseInfo(human)
end

function send_GC_HORSE_STAR_UP_ITEM_INFO(human)
  local sendMsg = Msg.gc.GC_HORSE_STAR_UP_ITEM_INFO
  local cache = {}
  sendMsg.item[0] = #needConfig
  for i=1, #needConfig do
    Grid.create(cache, needConfig[i].itemId, 1)
    Grid.makeItem(cache, sendMsg.item[i], 0, human)
  end
  Msg.send(sendMsg, human.fd)
end
--设置星星信息
function setXXInfo(human, stage, info)
  if human.db.horseXX[stage - 1] > 5 then
    return
  end
  info.stage = stage
  local cache = {}
  local tmpConfig = StageConfig[stage]
  local xxCnt = human.db.horseXX[stage - 1]  
  calCurAddAttr(human, info.curAttr)
  info.curPower = getZhandouli(human, info.curAttr, StageConfig[stage].horseId)
  info.xxCnt = xxCnt
  if xxCnt < 5 and tmpConfig then    
    calCurAddAttr(human, info.nextAttr)
    if xxCnt > 0 then
      for i=1, #tmpConfig.xxAttr[xxCnt] do
        local realAddAttr = {}
        realAddAttr[1] = tmpConfig.xxAttr[xxCnt+1][i][1]
        realAddAttr[2] = tmpConfig.xxAttr[xxCnt+1][i][2] - tmpConfig.xxAttr[xxCnt][i][2]
        RoleAttr.addOneAttr(info.nextAttr, realAddAttr)
      end
    else
      addAttr(info.nextAttr, tmpConfig.xxAttr[xxCnt+1])
    end
    info.nextPower = getZhandouli(human, info.nextAttr, StageConfig[stage].horseId)
    info.needYinliang = tmpConfig.xxNeedYinliang[xxCnt + 1]
    info.needItem[0] = 1
    Grid.create(cache, tmpConfig.xxNeedItem[xxCnt + 1][1], tmpConfig.xxNeedItem[xxCnt + 1][2])
    Grid.makeItem(cache, info.needItem[1], 0, human)
  else
    info.nextAttr[0] = 0
    info.nextPower = 0
    info.needYinliang = 0
    info.needItem[0] = 0
  end
  if xxCnt == 5 then
    info.needItem[0] = 1
    Grid.create(cache, tmpConfig.xxNeedItem[xxCnt][1], tmpConfig.xxNeedItem[xxCnt][2])
    Grid.makeItem(cache, info.needItem[1], 0, human)
  end
end
--得到某个坐骑星星
function getHorseXX(human, id)
  for i=1, #StageConfig do
    if StageConfig[i].horseId == id then
      return human.db.horseXX[i - 1]
    end
  end
  for k,v in pairs(HuanhuaConfig) do
    if k == id then
      return getHuanhuaHorseInfo(human, id).horseXX 
    end
  end 
end
--更新单个等阶的坐骑星星
function updateXX(human, stage)
  local sendMsg = Msg.gc.GC_HORSE_XX_UPDATE
  setXXInfo(human, stage, sendMsg.info)
  Msg.send(sendMsg, human.fd)
end
--发送所有等阶坐骑星星
function sendXXList(human)
  local sendMsg = Msg.gc.GC_HORSE_XX_LIST
  sendMsg.list[0] = #StageConfig 
  for i=1,#StageConfig do
    setXXInfo(human, i, sendMsg.list[i])
  end
  Msg.send(sendMsg, human.fd)
end
--幻化坐骑升星星
function HhXxUp(human, huanhuaId)
  local data = getHuanhuaHorseInfo(human, huanhuaId)
  if not data then
    return
  end
  local tmpConfig = HuanhuaConfig[huanhuaId]
  local xxCnt = data.horseXX + 1
  if ObjHuman.getYinliang(human) < tmpConfig.xxNeedYinliang[xxCnt] then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_XX_YINLIANG_NOT_ENOUGH)
  end
  if BagLogic.getItemCount(human, tmpConfig.xxNeedItem[xxCnt][1]) < tmpConfig.xxNeedItem[xxCnt][2] then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_XX_ITEM_NOT_ENOUGH)
  end
  BagLogic.delItemsById(human, tmpConfig.xxNeedItem[xxCnt][1], tmpConfig.xxNeedItem[xxCnt][2], "horse")
  ObjHuman.decYinliang(human, tmpConfig.xxNeedYinliang[xxCnt], "horse")
  data.horseXX = data.horseXX + 1
  sendHorseInfo(human)
  updateHuanhua(human, huanhuaId)  
  ObjHuman.doCalc(human)
  local str = string.format(Lang.HORSE_STAR_BROADCAST, HtmlUtil.fontName(human.name, human._id), tmpConfig.name, data.horseXX , human._id)
  Broadcast.sendBroadcast2World(Lang.UP+Lang.GROSSIP, str)
  Log.write(Log.LOGID_OSS_HORSE_XX, human._id, human.account, human.name, human.db.lv, huanhuaId, data.horseXX)
end
--等阶坐骑升星星
function xxUp(human, stage)
  local tmpConfig = StageConfig[stage]
  local xxCnt = human.db.horseXX[stage - 1] + 1
  if stage > human.db.horseStageLv then
    return --Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_XX_STAGE_NOT_ENOUGH)
  end
  if human.db.horseXX[stage - 1] >= 5 then
    return
  end
  if ObjHuman.getYinliang(human) < tmpConfig.xxNeedYinliang[xxCnt] then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_XX_YINLIANG_NOT_ENOUGH)
  end
  if BagLogic.getItemCount(human, tmpConfig.xxNeedItem[xxCnt][1]) < tmpConfig.xxNeedItem[xxCnt][2] then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_XX_ITEM_NOT_ENOUGH)
  end
  BagLogic.delItemsById(human, tmpConfig.xxNeedItem[xxCnt][1], tmpConfig.xxNeedItem[xxCnt][2], "horse")
  ObjHuman.decYinliang(human, tmpConfig.xxNeedYinliang[xxCnt], "horse")
  human.db.horseXX[stage - 1] = human.db.horseXX[stage - 1] + 1
  updateXX(human, stage)
  sendHorseList(human)
  sendHorseInfo(human)
  ObjHuman.doCalc(human)
  local str = string.format(Lang.HORSE_STAR_BROADCAST, HtmlUtil.fontName(human.name, human._id), tmpConfig.name, human.db.horseXX[stage - 1], human._id)
  Broadcast.sendBroadcast2World(Lang.UP+Lang.GROSSIP, str)
  Log.write(Log.LOGID_OSS_HORSE_XX, human._id, human.account, human.name, human.db.lv, stage, human.db.horseXX[stage - 1])
end

--坐骑技能升级
function onSkillUpGrade(human, index, skillID)
  if index == 1 then
    human.db.horseSkillLv = human.db.horseSkillLv + 1
    sendHorseList(human)
  else
    for k,v in pairs(HuanhuaConfig) do
      if v.skillId == skillID then
        local data = getHuanhuaHorseInfo(human, k)
        data.skillLv = data.skillLv + 1
        updateHuanhua(human, data.haveHorseId)
        break  
      end
    end
  end
  sendHorseInfo(human)
  ObjHuman.doCalc(human)
  SkillNotify.GC_SKILL_LIST(human)
  SkillNotify.GC_SKILL_LIST_CHANGE(human)
  SkillNotify.GC_SKILL_MAIN_MENU(human)
end

function setHorseShowInfo(info, id)  
  for k,v in pairs(HuanhuaConfig) do
    if k == id then
      info.id = v.source
      info.effect = v.effect
      info.attr[0] = #v.addAttr
      for i=1, #v.addAttr do
        info.attr[i].key = v.addAttr[i][1]
        info.attr[i].value = v.addAttr[i][2]
      end
      info.type = 1
      info.power = getZhandouli(nil, info.attr, id)
    end
  end   
end
--得到坐骑装备data
function getHorseData(human, id)
  for i=1, #StageConfig do
    if StageConfig[i].horseId == id and i <= human.db.horseStageLv then
      return human.db.stageHorse[StageConfig[i].horseId - 1]
    end
  end
  return getHuanhuaHorseInfo(human, id)  
end

function equipUpQua(human, horseId)
  local data = getHorseData(human, horseId)  
  if not data or data.equipQua >= 15 then
    return
  end
  for i=0, 3 do
    if data.equipId[i] == 0 then --~= HorseEquipQua[data.equipQua + 1].needEquip[i]  then
      return
    end
  end
  data.equipQua = data.equipQua + 1
  for i=0, 3 do
    data.equipId[i] = 0
  end
  if getHuanhuaHorseInfo(human, horseId) then
    updateHuanhua(human, horseId)
  else
    sendHorseList(human)
  end      
  sendHorseInfo(human)      
  ObjHuman.doCalc(human)
  if data.equipQua > 1 then
    local str = string.format(Lang.HORSE_EQUIP_PRO, HtmlUtil.fontName(human.name, human._id), getCommon(human, horseId).name, ItemDefine.getText(data.equipQua))
    Broadcast.sendBroadcast2World(Lang.UP+Lang.GROSSIP, str)
  end
end

function equipHeCheng(human, horseId, equipId)
  local data = getHorseData(human, horseId)  
  if not data then
    return
  end
  for i=0, 3 do
    if data.equipId[i] == equipId then      
      return
    end
  end
  local checkIndex = -1
  for i=0,3 do
    if HorseEquipQua[data.equipQua + 1].needEquip[i + 1] == equipId then
      checkIndex = i
      break
    end
  end
  if checkIndex == -1 then
    return 
  end
  
  if ObjHuman.getYinliang(human) < HorseEquipHecheng[equipId].needYinliang then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_HECHENG_YINLIANG_NOT_ENOUGH)
  end
  for j=1, #HorseEquipHecheng[equipId].needItem do
    if BagLogic.getItemCount(human, HorseEquipHecheng[equipId].needItem[j][1]) < HorseEquipHecheng[equipId].needItem[j][2] then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_HECHENG_ITEM_NOT_ENOUGH)
    end
  end
  if getHorseLv(human) < HorseEquipHecheng[equipId].needHorseLv then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.HORSE_HECHENG_LV_NOT_ENOUGH)
  end
  
  for j=1, #HorseEquipHecheng[equipId].needItem do
    BagLogic.delItemsById(human, HorseEquipHecheng[equipId].needItem[j][1], HorseEquipHecheng[equipId].needItem[j][2], "horse")
  end
  ObjHuman.decYinliang(human, HorseEquipHecheng[equipId].needYinliang, "horse")
  data.equipId[checkIndex] = equipId
  Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_EQUIP_HECHENG_OK)
  if getHuanhuaHorseInfo(human, horseId) then
    updateHuanhua(human, horseId)
  else
    sendHorseList(human)
  end      
  sendHorseInfo(human)      
  ObjHuman.doCalc(human)
end

-- 是否培养已满
function isFeedFull(human, id, config, data)
    if not data then
      return
    end
    local len = #config.feedAttr
    if len > 8 then
        len = 8
    end
    for i = 1, len do
        if data.feedAttr[i-1] < config.feedAttr[i][2] then
            return
        end
    end
    return true
end
--计算培养值
function randFeed(human,  id, config, data, len)
    if isFeedFull(human, id, config, data) then
        return
    end

    local process = 0
    for i = 1, len do
        process = process + data.feedAttr[i-1]/config.feedAttr[i][2]
    end
    process = math.floor(process / len * 100)

    local feed
    for i = 1, #feedConfig do
        if process < feedConfig[i].progress then
            feed = feedConfig[i]
            break
        end
    end
    if not feed then
        return
    end

    local attrCnt
    local sum = 0
    for i = 1, len do
        sum = sum + (feed.weight[i] or 0)
    end
    local randVal = math.random(1, sum)
    sum = 0
    for i = 1, len do
        sum = sum + (feed.weight[i] or 0)
        if sum >= randVal then
            attrCnt = i
            break
        end
    end
    local ret = {}
    local start = math.random(1,  len)
    local realCnt = 0
    for i = 1, len do
        if ret[config.feedAttr[start][1]] == nil and data.feedAttr[start-1] < config.feedAttr[start][2] and (attrCnt >= len or math.random(1, 10000)%2  == 1) then
            ret[config.feedAttr[start][1]] = math.random(config.feedAttr[start][3], config.feedAttr[start][4])
            realCnt = realCnt + 1
        end
        start = start + 1
        if start > len then
            start = 1
        end
        if realCnt == attrCnt then
            break
        end
    end
    if realCnt < attrCnt then --再算一篇，无随机属性的
        for i = 1, len do
            if ret[config.feedAttr[start][1]] == nil and data.feedAttr[start-1] < config.feedAttr[start][2] then
                ret[config.feedAttr[start][1]] = math.random(config.feedAttr[start][3], config.feedAttr[start][4])
                realCnt = realCnt + 1
            end
            start = start + 1
            if start > len then
                start = 1
            end
            if realCnt == attrCnt then
                break
            end
        end
    end

    if realCnt > 0 then
        return ret
    end
end

--培养数据查询
function feedQuery(human, id, add)
    local config = getCommon(human, id)
    if not config then
        return
    end
    local data = getHorseData(human, id)
    if not data then
        return
    end
    local msg = Msg.gc.GC_HORSE_FEED_QUERY
    msg.id = id
    local len = #config.feedAttr
    if len > 8 then
        len = 8
    end
    for i = len + 1, 8 do
        data.feedAttr[i-1] = 0
    end
    local full =  isFeedFull(human, id, config, data)
    for i = 1, len do
        msg.feedAttr[i].key = config.feedAttr[i][1]
        msg.feedAttr[i].value = add and add[config.feedAttr[i][1]] and data.feedAttr[i-1]-add[config.feedAttr[i][1]] or data.feedAttr[i-1]
        if full or data.feedAttr[i-1] >= config.feedAttr[i][2] then
            msg.feedAddVal[i] = -1
        else
            msg.feedAddVal[i] = add and add[config.feedAttr[i][1]] or 0
        end
    end
    msg.feedAttr[0] = len
    msg.feedAddVal[0] = len
    Msg.send(msg, human.fd)
end

--神兵培养
function feed(human, id)
    local config = getCommon(human, id)
    if not config then
        return
    end
    local data = getHorseData(human, id)
    if not data then
        return
    end
    if getHuanhuaHorseInfo(human, id) then
      if data.horseXX ~= 5 then
        return
      end
    else
      if human.db.horseXX[id - 1] ~= 5 then
        return
      end
    end
    if ObjHuman.getYinliang(human) < HorseDefine.FEED_NEED_YINLIANG then
      return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.YINLIANG_NOT_ENOUGH)
    end
    if BagLogic.getItemCount(human, config.xxNeedItem[1][1]) < 1 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, string.format(Lang.HORSE_FEED_NO_MATRAIL, ItemDefine.getValue(config.xxNeedItem[1][1], "name")))
        return
    end
    local len = #config.feedAttr
    if len > 8 then
        len = 8
    end
    local add = randFeed(human, id, config, data, len)
    if not add then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_FEED_FULL)
        return
    end
    ObjHuman.decYinliang(human, HorseDefine.FEED_NEED_YINLIANG,"horse_feed")
    BagLogic.delItemsById(human, config.xxNeedItem[1][1], 1, "horse_feed")
    for i = 1, len do
        if add[config.feedAttr[i][1]] then
            data.feedAttr[i-1] = data.feedAttr[i-1] + add[config.feedAttr[i][1]]
            if data.feedAttr[i-1] > config.feedAttr[i][2] then
                data.feedAttr[i-1] = config.feedAttr[i][2]
            end
        end
    end
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_FEED_OK)
    if getHuanhuaHorseInfo(human, id) then
      updateHuanhua(human, id)
    else
      sendHorseList(human)
    end      
    sendHorseInfo(human) 
    feedQuery(human, id, add)
    ObjHuman.doCalc(human)
end

function feedMulti(human, id, cnt)
    if cnt > 99 then
        cnt = 99
    end
    local config = getCommon(human, id)
    if not config then
        return
    end
    local data = getHorseData(human, id)
    if not data then
        return
    end
    if getHuanhuaHorseInfo(human, id) then
      if data.horseXX ~= 5 then
        return
      end
    else
      if human.db.horseXX[id - 1] ~= 5 then
        return
      end
    end
    if ObjHuman.getYinliang(human) < HorseDefine.FEED_NEED_YINLIANG * cnt then
      return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.YINLIANG_NOT_ENOUGH)
    end
    local hadCnt = BagLogic.getItemCount(human, config.xxNeedItem[1][1])
    if hadCnt < 1 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, string.format(Lang.HORSE_FEED_NO_MATRAIL, ItemDefine.getValue(config.xxNeedItem[1][1], "name")))
        return
    end
    if cnt > hadCnt then
        cnt = hadCnt
    end
    local len = #config.feedAttr
    if len > 8 then
        len = 8
    end
    local add = randFeed(human, id, config, data, len)
    if not add then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.HORSE_FEED_FULL)
        return
    end
    
    local del = 0
    local totalAdd = {}
    for k = 1, cnt do
         if not add then
            break
         end      
         for i = 1, len do
           if add[config.feedAttr[i][1]] then
              data.feedAttr[i-1] = data.feedAttr[i-1] + add[config.feedAttr[i][1]]
              if data.feedAttr[i-1] > config.feedAttr[i][2] then
                data.feedAttr[i-1] = config.feedAttr[i][2]
              end
           end
         end        
         del =  del + 1 
         for kk,vv in pairs(add) do
            if totalAdd[kk] then
                totalAdd[kk] = totalAdd[kk] + vv
            else
                totalAdd[kk] = vv
            end
         end
         add = randFeed(human, id, config, data, len)
    end
    ObjHuman.decYinliang(human, HorseDefine.FEED_NEED_YINLIANG * del,"horse_feed")
    BagLogic.delItemsById(human, config.xxNeedItem[1][1], del, "horse_feed")
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.SHENBING_FEED_OK)
    feedQuery(human, id, totalAdd)
    if getHuanhuaHorseInfo(human, id) then
      updateHuanhua(human, id)
    else
      sendHorseList(human)
    end      
    sendHorseInfo(human) 
    ObjHuman.doCalc(human)
    
    local mm = Msg.gc.GC_HORSE_MULTI_FEED
    mm.id = id
    mm.cnt = del
    Msg.send(mm, human.fd)
end

function sendGC_HORSE_HEIGHT(human)
  local sendMsg = Msg.gc.GC_HORSE_HEIGHT
  local len = 0  
  for i=1, #StageConfig do
    len = len + 1
    sendMsg.heightInfo[len].source = StageConfig[i].source
    sendMsg.heightInfo[len].height = StageConfig[i].height
  end
  for k,v in pairs(HuanhuaConfig) do
    len = len + 1
    sendMsg.heightInfo[len].source = v.source
    sendMsg.heightInfo[len].height = v.height
  end
  sendMsg.heightInfo[0] = len
  Msg.send(sendMsg, human.fd)
end
